#include <stdio.h>
#include <stdlib.h>

/*
* I_3.c
*
*  Created on: April, 19, 2009
*      Author: yimingliu@fudan.edu.cn
*/

typedef struct aa
{
	int data;
	struct aa *next;
} NODE;

int funI_3_fun(NODE *h)
{
	int even=0;
	NODE *p;
	p=h->next;
	while(p)
	{
		if(p->data%2==0)
			even+=p->data;
		p=p->next;
	}
	return even;
}

NODE *createlink(int n)
{
	NODE *h,*p,*s;
	int i;
	h=p=(NODE*)malloc(sizeof(NODE));
	for(i=1;i<=n;i++)
	{
		s=(NODE*)malloc(sizeof(NODE));
		s->data=rand()%16;
		s->next=p->next;
		p->next=s;
		p=p->next;
	}
	p->next=NULL;
	return h;
}

void outlink(NODE *h,FILE *pf)
{
	NODE *p;
	p=h->next;
	fprintf(pf,"\n\nTHE LIST: \n\n HEAD");
	while(p)
	{
		fprintf(pf,"->%d",p->data);
		p=p->next;
	}
	fprintf(pf,"\n");
}

void outresult(int s,FILE *pf)
{
	fprintf(pf,"\nThe sum of even numbers : %d\n",s);
}

void funI_3()
{
	NODE *head;int even;
	head=createlink(12);
	head->data=9000;
	outlink(head,stdout);
	even=funI_3_fun(head);
	printf("\nThe result :\n");
	outresult(even,stdout);
}